package com.chen.service.role;

import com.chen.dao.RoleMapper;
import com.chen.pojo.Role;
import com.chen.vo.RoleVo;

import java.util.List;
import java.util.Map;

public class RoleServiceImpl implements RoleService {
    private RoleMapper roleMapper;

    public void setRoleMapper(RoleMapper roleMapper) {
        this.roleMapper = roleMapper;
    }

    public Role queryRoleById(int roleId) {
        return roleMapper.queryRoleById(roleId);
    }

    public List<Role> queryAllRole() {
        return roleMapper.queryAllRole();
    }

    public List<Role> queryRole(RoleVo roleVo) {
        return roleMapper.queryRole(roleVo);
    }

    public int addRole(Role role) {
        return roleMapper.addRole(role);
    }

    public int updateRole(Role role) {
        return roleMapper.updateRole(role);
    }

    public int deleteById(int roleId) {
        return roleMapper.deleteById(roleId);
    }

    public int saveRoleMenu(String ids, Integer roleId) {
        try {
            //删除原有的菜单关系
            roleMapper.deleteRoleMenu(roleId);//根据角色ID删除
            //将ids拆分成数组
            String [] idsStr = ids.split(",");
            //循环调用
            for (int i = 0; i <idsStr.length ; i++) {
                //调用保存菜单角色关系的方法
                roleMapper.addRoleMenu(roleId,idsStr[i]);
            }
            return 1;//成功
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;//失败
    }

    public List<Map<String, Object>> queryRoleListByMap() {
        return roleMapper.queryRoleListByMap();
    }

    public List<Integer> queryEmployeeRoleByVipId(Integer vipId) {
        return roleMapper.queryEmployeeRoleByVipId(vipId);
    }


}
